package com.mdlib.helper.ui.fragments.rush.money;

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ljlib.core.util.LogUtil;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.entity.RunInfo;
import com.mdlib.helper.ui.fragments.sect.money.MoneyRelateFragment;
import com.mdlib.helper.ui.fragments.sect.money.WithdrawalApplyFragment;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

import static com.mdlib.helper.ui.UIHelper.BUDGET;
import static com.mdlib.helper.ui.UIHelper.WITHDRAWAL;

/**
 * 作者：HQY on 16/11/30 09:26
 * 邮箱：hqy_xz@126.com
 */

public class RushBalanceFragment extends BaseTitleFragment {

    @Bind(R.id.rl_rush_account_details)
    RelativeLayout mRlRushAccountDetails;
    @Bind(R.id.tv_rush_balance_pay)
    TextView mTvRushBalancePay;
    @Bind(R.id.tv_rush_balance_apply)
    TextView mTvRushBalanceApply;
    @Bind(R.id.tv_rush_account_money)
    TextView mTvRushAccountMoney;
    @Bind(R.id.tv_rush_account_day_money)
    TextView mTvRushAccountDayMoney;
    @Bind(R.id.tv_rush_account_earnings_money)
    TextView mTvRushAccountEarningsMoney;
    @Bind(R.id.tv_rush_account_withdraw_money)
    TextView mTvRushAccountWithdrawMoney;

    private String money = "0";

    public static RushBalanceFragment newInstance() {
        return new RushBalanceFragment();
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_rush_balance;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.balance_account_money, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });

    }

    @Override
    public void onStart() {
        super.onStart();
        getRunInfo();
    }

    /**
     * 跑客消息
     */
    private void getRunInfo() {
        addSubscribe(DataManager.getInstance().getUserApi()
                .getRunInfo()
                .compose(RxHelper.<RunInfo>defaultHandleResult())
                .subscribe(new RxSubscribe<RunInfo>() {
                    @Override
                    protected void onSucc(RunInfo runInfo) {
                        money = runInfo.getBalance();
                        LogUtil.e(runInfo.getMoney());
                        mTvRushAccountMoney.setText("￥" + runInfo.getBalance());
                        mTvRushAccountDayMoney.setText("￥" + runInfo.getMoney());
                        mTvRushAccountEarningsMoney.setText("￥" + runInfo.getSum_money());
                        mTvRushAccountWithdrawMoney.setText("￥" + runInfo.getWithdraw());
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));
    }


    @OnClick({R.id.rl_rush_account_details, R.id.tv_rush_balance_pay, R.id.tv_rush_balance_apply})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_rush_account_details:
                addFragment(MoneyRelateFragment.newInStance(BUDGET));
                break;
            case R.id.tv_rush_balance_pay:
                addFragment(WithdrawalApplyFragment.newInstance(money));
                break;
            case R.id.tv_rush_balance_apply:
                addFragment(MoneyRelateFragment.newInStance(WITHDRAWAL));
                break;
        }
    }
}
